home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-30 | 5.4 KB | 213 lines | [TEXT/MPS ] |
- /*
- File: OTMulticastcatch.cp
-
- Contains: Sample program to receive UDP/IP multicasts.
-
- Copyright: © 1993-1996 by Apple Computer, Inc., all rights reserved.
-
-
- */
-
-
- #include <OpenTptInternet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <Events.h>
- #include <Quickdraw.h>
-
-
- /*******************************************************************************
- ** Globals
- ********************************************************************************/
-
- char gMulticastAddrStr[] = "235.1.2.3";
- InetPort gMulticastPort = 2345;
- InetHost gMulticastHost;
- InetAddress gMulticastAddr;
- Boolean gDataEvent = false;
- EndpointRef gEndpt;
- char gMessage[256];
-
- /*******************************************************************************
- ** EventHandler
- ********************************************************************************/
-
- pascal void EventHandler(void*, OTEventCode event, OTResult, void*)
- {
- if (event == T_DATA)
- gDataEvent = true;
- return;
- }
-
-
- /*******************************************************************************
- ** SetupMulticast
- ********************************************************************************/
-
- OSStatus SetupMulticast()
- {
- TEndpointInfo info;
- OSStatus err = kOTNoError;
- InetAddress myAddr;
-
- fprintf(stderr, "The program receives multicasts on IPAddr <%s> port <%d>\n",
- gMulticastAddrStr, gMulticastPort);
-
- do
- {
- //
- // Get a UDP endpoint which can be used for IP Multicast receives
- //
-
- gEndpt = OTOpenEndpoint(OTCreateConfiguration(kUDPName), 0, &info, &err);
- if ( gEndpt == NULL || err != kOTNoError )
- {
- fprintf(stderr, "ERROR: OpenEndpoint(UDP) failed with error <%d>\n", err);
- break;
- }
-
- //
- // Set up an Inet Address for the multicast group
- // using our multicast address string and defined port number.
- //
-
- OTInetStringToHost(gMulticastAddrStr, &gMulticastHost);
- OTInitInetAddress(&gMulticastAddr, gMulticastPort, gMulticastHost);
-
- //
- // Do a normal bind to the any IP addr on our system (by entering 0 as
- // a wildcard IP address) using the multicast port.
- // It's OK to use req for both input and output.
- //
-
- OTInitInetAddress(&myAddr, gMulticastPort, 0);
- TBind bindReq;
- bindReq.addr.len = sizeof(myAddr);
- bindReq.addr.maxlen = sizeof(myAddr);
- bindReq.addr.buf = (unsigned char*) &myAddr;
- err = gEndpt->Bind(&bindReq, &bindReq);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Bind() failed with error <%d>\n", err);
- break;
- }
-
- //
- // Let IP know to listen for this multicast IP address on all interfaces.
- //
-
- TOptMgmt optReq;
- UInt8 optBuffer[ kOTOptionHeaderSize + sizeof(TIPAddMulticast) ];
- TOption* opt = (TOption*)optBuffer;
- TIPAddMulticast* addopt = (TIPAddMulticast*)opt->value;
-
- optReq.flags = T_NEGOTIATE;
- optReq.opt.len = sizeof(optBuffer);
- optReq.opt.buf = (UInt8*) optBuffer;
-
- opt->level = INET_IP;
- opt->name = IP_ADD_MEMBERSHIP;
- opt->len = sizeof(optBuffer);
-
- addopt->multicastGroupAddress = gMulticastHost;
- addopt->interfaceAddress = kOTAnyInetAddress;
-
- err = gEndpt->OptionManagement(&optReq, &optReq);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: OTOptionManagement() failed with %d\n", err);
- break;
- }
-
- //
- // Now install a notifier for and switch to asynchronous mode.
- //
-
- err = gEndpt->InstallNotifier((OTNotifyProcPtr)&EventHandler, 0);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: InstallNotifier() failed with error <%d>\n", err);
- break;
- }
- err = gEndpt->SetAsynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetAsynchronous() failed with error <%d>\n", err);
- break;
- }
- } while(false);
- return ( err != kOTNoError );
- }
-
-
- /*******************************************************************************
- ** Cleanup
- ********************************************************************************/
-
- void Cleanup()
- {
- gEndpt->Close();
- }
-
-
- /*******************************************************************************
- ** RecvLoop
- ********************************************************************************/
-
- void RecvLoop()
- {
- TUnitData unitdata;
- InetAddress addr;
- OTFlags flags;
- char remoteAddrStr[32];
- char messageBuf[128];
-
- OTMemzero(messageBuf, sizeof(messageBuf));
- OTMemzero(remoteAddrStr, sizeof(remoteAddrStr));
- while ( !Button() )
- {
- if ( gDataEvent )
- {
- gDataEvent = false;
- unitdata.addr.maxlen = sizeof(addr);
- unitdata.addr.buf = (unsigned char*) &addr;
- unitdata.opt.maxlen = 0;
- unitdata.opt.buf = 0;
- unitdata.udata.maxlen = sizeof(gMessage);
- unitdata.udata.buf = (UInt8*) gMessage;
- gEndpt->RcvUData( &unitdata, &flags);
- OTInetHostToString( addr.fHost, remoteAddrStr);
- fprintf(stderr, "IP multicast from <%s>:%s\n", remoteAddrStr, gMessage);
- OTMemzero(messageBuf, unitdata.udata.len);
- OTMemzero(remoteAddrStr, sizeof(remoteAddrStr));
- }
- }
- }
-
-
- /*******************************************************************************
- ** Inits
- ********************************************************************************/
-
- void Inits()
- {
- InitGraf(&qd.thePort);
- if ( InitOpenTransport() != kOTNoError )
- {
- fprintf(stderr, "MulticastCatchSample: Could not initialize ASLM\n");
- exit(1);
- }
- }
-
-
- /*******************************************************************************
- ** Main
- ********************************************************************************/
-
- void main()
- {
- Inits();
- if ( SetupMulticast() == kOTNoError )
- RecvLoop();
- Cleanup();
- }